<?php
/**
 * Created by PhpStorm.
 * User: chenyuz
 * Date: 2019/3/5
 * Time: 15:41
 */
namespace app\ops\controller;

use app\tools\MIniprogram;
use think\Db;

class Company extends Base{
    function index(){

        $query = request()->get();
        $where = [];
        if($query['search']){
            $where['name'] = ['like', "%{$query['search']}%"];
        }

        if($query['category_id']){
            $where['category_id'] = $query['category_id'];
        }

        $list = Db::name("company")->where($where)->field(["id", "account", "mobile", "address", "tel", "status", "create_time", "qrcode", "name", "ad_img", "category_id"])->order("status, create_time desc")->paginate(10);
        $category = db('category')->field('name, category_id')->select();
        $this->assign("list", $list);
        $this->assign("category", array_column($category, 'name', 'category_id'));
        $this->assign("query", $query);
        return $this->fetch('company');
    }

    public function detail($id) {

        $detail = \db('company')->where(['id'=> $id])->find();
        if($detail['images'])
        {
            $detail['images'] = explode(',', $detail['images']);
        }else{
            $detail['images'] = [];
        }
        $type = \db('company_type')->select();

        $this->assign('data', $detail);
        $this->assign('type', array_column($type, 'name', 'id'));
        return $this->fetch();

    }

    function setstatus(){

        $id = request()->post('id', 0);
        $status = request()->post('status', 0);
        $reason = request()->post('reason', '');

        $post = ['status'=> $status];

        if($reason){
            $post['reason'] = $reason;
        }

        db('company')->where(['id'=> $id])->update($post);

        return success([]);
    }

    function edit($id){

        if($post = request()->post()){
            db('company')->update([
                'id'=> $post['id'],
                'qrcode'=> $post['qrcode'],
                'ad_img'=> $post['ad_img'],
                'category_id'=> $post['category_id'],
                'tag'=> $post['tag'],
                'cover'=> $post['cover'],
            ]);
            return success([]);
        }else{

            $data = db('company')->where(['id'=> $id])->field("id, qrcode,ad_img, category_id, cover, tag")->find();

            $category = db('category')->field('name, category_id')->select();
//            dump($data);
            $this->assign('data', $data);
            $this->assign('category', $category);
            return $this->fetch();
        }

    }

    function upload()
    {
        $file = request()->file('image');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->validate(['size'=>2097152])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 jpg
                $url = DS . 'uploads' . DS . $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                return fail("JPG_OVER");
            }
        } else {
            return fail();
        }

        return success(['url' => config('host') . $url]);

    }

    function slider($id)
    {
        $post = request()->post();
        if($post)
        {
            \db('company')->where(['id'=> $post['id']])->update([
                'images'=> implode(',', $post['images'])
            ]);

        }else{
            $data = db('company')->where(['id'=> $id])->field('images, id')->find();
            if($data['images'])
            {
                $data['images'] = explode(',', $data['images']);
            }else{
                $data['images'] = [];
            }

            $this->assign('data', $data);

            return $this->fetch();
        }
        return success([]);
    }
}